package model.mchoice;

import java.io.Serializable;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * Representa una pregunta de un set de preguntas de multiple choice;
 *
 * @author Jose Martin Rozanec;
 */
public class MCItem implements Serializable {
    private String question;
    private String[] solutions;
    private boolean[] solutionStatus;
    private int correctPunctuation;
    private int incorrectPunctuation;

    /**
     * Constructor;
     *
     * @param solutions - cantidad de soluciones a la pregunta;
     */
    public MCItem(int solutions) {
        this.solutions = new String[solutions];
        this.solutionStatus = new boolean[solutions];
    }

    /**
     * Permite obtener la pregunta;
     *
     * @return String;
     */
    public String getQuestion() {
        return question;
    }

    /**
     * Permite setear la pregunta;
     *
     * @param question - String;
     */
    public void setQuestion(String question) {
        this.question = question;
    }

    /**
     * Permite obtener las soluciones;
     *
     * @return String[] - soluciones;
     */
    public String[] getSolutions() {
        return solutions;
    }

    /**
     * Permite establecer las soluciones;
     *
     * @param solution - String;
     * @param answer   - numero de solucion;
     */
    public void setSolutions(String solution, int answer) {
        this.solutions[answer - 1] = solution;
    }

    /**
     * Permite obtener el estado de las soluciones. El estado puede ser true o false;
     *
     * @return boolean[] - estados de las soluciones;
     */
    public boolean[] getSolutionStatus() {
        return solutionStatus;
    }

    /**
     * Permite setear el estado de cada una de las soluciones;
     *
     * @param solutionStatus - boolean;
     * @param answer         - int posicion;
     */
    public void setSolutionStatus(boolean solutionStatus, int answer) {
        this.solutionStatus[answer - 1] = solutionStatus;
    }

    /**
     * Permite obtener la puntuacion en caso de resolucion correcta;
     *
     * @return int;
     */
    public int getCorrectPunctuation() {
        return correctPunctuation;
    }

    /**
     * Permite establecer la puntuacion para el caso de resolucion correcta;
     *
     * @param correctPunctuation;
     */
    public void setCorrectPunctuation(int correctPunctuation) {
        this.correctPunctuation = correctPunctuation;
    }

    /**
     * Permite obtener la puntuacion en caso de resolucion incorrecta;
     *
     * @return int;
     */
    public int getIncorrectPunctuation() {
        return -incorrectPunctuation;
    }

    /**
     * Permite establecer la puntuacion para el caso de resolucion incorrecta;
     *
     * @param incorrectPunctuation;
     */
    public void setIncorrectPunctuation(int incorrectPunctuation) {
        this.incorrectPunctuation = Math.abs(incorrectPunctuation);
    }

    /**
     * Returns the result of the MCItem, acording to the solution presented;
     *
     * @param solution - must be boolean[4];
     * @return double resulting punctuation;
     */
    public double getResult(boolean[] solution) {
        boolean correct = true;
        for (int j = 0; j < solutions.length; j++) {
            if (solution[j] != solutionStatus[j]) {
                return -Math.abs(incorrectPunctuation);
            }
        }
        return correctPunctuation;
    }

    /**
     * Devuelve la representacion en String;
     *
     * @return String;
     */
    public String toString() {
        StringBuffer buffer = new StringBuffer();
        buffer.append(question + "\t\t [" + getCorrectPunctuation() + "] " +
                "[" + getIncorrectPunctuation() + "]" + "\n");
        buffer.append("" + ((char) (65)) + "- " + solutions[0] + "\t [" + getSolutionStatus()[0] + "]" + "\n");
        for (int j = 1; j < solutions.length; j++) {
            buffer.append("" + ((char) (65 + j)) + "- " + solutions[j] + "\t [" + getSolutionStatus()[j] + "]" + "\n");
        }
        return buffer.toString();
    }

    /**
     * Clona la instancia, sin copiar los estados de los resultados;
     *
     * @return MCItem;
     */
    public MCItem getDefaultClone() {
        MCItem clone = new MCItem(this.getSolutions().length);
        clone.setQuestion(this.getQuestion());
        clone.setCorrectPunctuation(this.getCorrectPunctuation());
        clone.setIncorrectPunctuation(this.getIncorrectPunctuation());
        for (int j = 0; j < this.getSolutions().length; j++) {
            clone.setSolutions(getSolutions()[j], j + 1);
            clone.setSolutionStatus(false, j + 1);
        }
        return clone;
    }

    public static void main(String[] args) {
        MCItem mcItem = new MCItem(4);
        mcItem.setQuestion("Pregunta?");
        mcItem.setSolutions("Solution1", 1);
        mcItem.setSolutions("Solution2", 2);
        mcItem.setSolutions("Solution3", 3);
        mcItem.setSolutions("Solution4", 4);
        mcItem.setCorrectPunctuation(10);
        mcItem.setIncorrectPunctuation(-5);
        System.out.println("" + mcItem.toString());
    }
}
